import smtplib
import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.utils import formataddr

# 配置信息
qq_mail = '793757152@qq.com'  # 替换为你的QQ邮箱
qq_password = 'wnqbdnxyzqphbfeh'  # 替换为QQ邮箱授权码
recipients = ['yrqc53719@163.com', '793757152@qq.com']  # 收件人列表

# 创建邮件对象
message = MIMEMultipart()
message['From'] = formataddr(('发送者名称', qq_mail))
message['To'] = ", ".join(recipients)  # 所有收件人可见彼此邮箱
message['Subject'] = '群发测试邮件'

# 邮件正文
message.attach(MIMEText('这是一封群发测试邮件，所有收件人能看到彼此的邮箱地址。', 'plain', 'utf-8'))

# 添加附件（示例）
with open('2. 群发邮件（所有收件人看到彼此邮箱）.py', 'rb') as f:
    attach_part = MIMEApplication(f.read(), Name='2. 群发邮件（所有收件人看到彼此邮箱）.py')
    attach_part['Content-Disposition'] = 'attachment; filename="2. 群发邮件（所有收件人看到彼此邮箱）.py"'
    message.attach(attach_part)

# 发送邮件
try:
    context = ssl.create_default_context()
    with smtplib.SMTP_SSL('smtp.qq.com', 465, context=context) as server:
        server.login(qq_mail, qq_password)
        server.sendmail(qq_mail, recipients, message.as_string())  # 关键点：收件人列表
        print(f"✅ 邮件已群发给 {len(recipients)} 人")
except Exception as e:
    print(f"❌ 发送失败: {e}")